#include "remoteform.h"
#include "ui_remoteform.h"

#include <models/robotlist.h>

#include <mainwindow.h>
#include <urdfwidget.h>

RemoteForm::RemoteForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RemoteForm)
{
    ui->setupUi(this);
    m_pRadioBtn=new QRadioButton();
    m_pButtonGroup=new QButtonGroup();
    m_pButtonGroup->addButton(m_pRadioBtn,-1);//只是为了在界面隐藏的时候其它按钮不被选中状态由于QButtonGroup必须选中一个
    m_pButtonGroup->addButton(ui->visionpro_btn,STATE_VISIONPRO);
    m_pButtonGroup->addButton(ui->xscebe_btn,STATE_XSENSE);
    //m_pButtonGroup->addButton(ui->quest_btn,STATE_QUEST);

    QString style= QString("QRadioButton::indicator:unchecked{\
                  width: %2px; \
                  height: %2px; \
                  margin-top:%3px; \
            border-image:url(:/images/uncheck.png);}\
            QRadioButton::indicator:checked{\
                      width: %2px; \
                             height: %2px; \
                                    margin-top:%3px; \
            border-image:url(:/images/check.png);}\
            QRadioButton{ \
            color: #FFFFFF; \
            font:%1px \"微软雅黑\";}").arg(int(18*g_xScale)).arg(int(20*g_xScale)).arg(int(0*g_xScale));
    ui->visionpro_btn->setStyleSheet(style);
    ui->xscebe_btn->setStyleSheet(style);
    //ui->quest_btn->setStyleSheet(style);
    QString strButtonStyle = QString("     QPushButton {\
        background-color: #007BFF;\
        color: white; \
        border-radius: 5px; \
        padding: 10px 20px;  \
        margin :0px 10px 0px 0px;\
        font: %1px \"微软雅黑\" \
    } \
   QPushButton:pressed { \
        background-color: #0056b3; \
    }").arg(20*g_xScale);
    ui->start_btn->setStyleSheet(strButtonStyle);
    ui->stop_btn->setStyleSheet(strButtonStyle);
    ui->start_btn->hide();
    ui->stop_btn->hide();

    //连接信号与槽
    connect(m_pButtonGroup, SIGNAL(buttonReleased(int)), this,SLOT(onBtnIdClicked(int)));
    initVideoStyle();
}

RemoteForm::~RemoteForm()
{
    delete ui;
}

void RemoteForm::initVideoStyle()
{
    connect(RobotList::getInstance(), &RobotList::curRobotChanged, this, [&](Robot * pCurRobot, Robot * pLastRobot){
         if(pLastRobot)
             disconnect(pLastRobot,NULL, this,NULL);
        m_listVideoInfo.clear();
        m_vtVideoWidget.clear();
        if(pCurRobot)
        {
            QString btnStyle = QString("QPushButton {"
                               "    color: white;"  // 字体颜色设置为白色
                               "    padding: 5px;"  // 设置内边距，使文本不紧贴边框
                               "    border: none;"  // 去除边框
                               "    font:%1px \"微软雅黑\";"
                               "}"
                               "QPushButton:unchecked {"
                               "    background-color: gray;"  // 未选中时的背景色
                               "}"
                               "QPushButton:checked {"
                               "    background-color: #098CFF;"  // 选中时的背景色
                                       "}").arg(int(18*g_xScale));
            // 创建一个垂直布局
            QVBoxLayout *mainLayout = new QVBoxLayout(ui->video_widget);
            mainLayout->setContentsMargins(20*g_xScale,20*g_xScale,0,0);
            QHBoxLayout *buttonLayout = new QHBoxLayout;
            buttonLayout->setContentsMargins(0,0,0,0);
            buttonLayout->setSpacing(0);
            m_listVideoInfo=pCurRobot->lsVideo();
            QButtonGroup * pGroup = new QButtonGroup(this);
            bool bFirst=true;
            for(auto videoInfo:m_listVideoInfo)
            {
                TouchButton* btn = new TouchButton(videoInfo.strName);
                btn->setStyleSheet(btnStyle);
                btn->setFixedWidth(100*g_xScale);
                btn->raise();
                btn->setCheckable(true);
                buttonLayout->addWidget(btn);
                if(bFirst)
                {
                    bFirst=false;
                    btn->setChecked(true);
                }
                pGroup->addButton(btn);
                connect(btn, &QPushButton::toggled,[this,btn,videoInfo](bool checked){
                    if(checked)
                        {
                    QString clickedButtonText = btn->text();
                    if (clickedButtonText == videoInfo.strName) {
                        for(auto video:m_vtVideoWidget){
                            if(video->strUrl()==videoInfo.strUrl)
                            {
                                video->setParent(ui->video_widget);
                                video->setGeometry(0,0,ui->video_widget->width(),ui->video_widget->height());
                                video->lower();
                                video->setShowMax(false);
                                video->show();
                            }
                            else
                                video->hide();
                        }
                    }
                    }
                });
                VideoBase *videoWidget= MainWindow::getInstance()->pVideoWidget(videoInfo.strUrl);
                m_vtVideoWidget.push_back(videoWidget);
            }
            buttonLayout->addStretch();
            mainLayout->addLayout(buttonLayout);
            mainLayout->addStretch();
        }
    });
}

void RemoteForm::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
    UrdfWidget *pUrf= MainWindow::getInstance()->pUrdfWidget();
    if(pUrf)
    {
        pUrf->setParent(ui->urdf_widget);
        pUrf->show();
        pUrf->setGeometry(0,0,ui->urdf_widget->width(),ui->urdf_widget->height());
    }
    if(m_vtVideoWidget.size()>0)
    {
        m_vtVideoWidget[0]->setParent(ui->video_widget);
        m_vtVideoWidget[0]->setGeometry(0,0,ui->video_widget->width(),ui->video_widget->height());
        m_vtVideoWidget[0]->lower();
        m_vtVideoWidget[0]->setShowMax(false);
        m_vtVideoWidget[0]->show();
    }
}

void RemoteForm::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event)
    m_pRadioBtn->setChecked(true);
    ui->start_btn->hide();
    ui->stop_btn->hide();
}

void RemoteForm::onBtnIdClicked(int nId)
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
    {
        pCurRobot->setTeleoperation(nId);
        ui->start_btn->show();
        ui->stop_btn->show();
    }
}

void RemoteForm::on_start_btn_clicked()
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
        pCurRobot->startTeleoperation();
}


void RemoteForm::on_stop_btn_clicked()
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
        pCurRobot->stopTeleoperation();
}

